﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VolunteerHubAdapter
{
    public class Adapter : OmaticIO.Base.Import.ImportSourceBase, IDisposable
    {
        public override string Author
        {
            get { return "Omatic Software"; }
        }

        public override string Description
        {
            get { return "Import connector for VolunteerHub"; }
        }

        private Models.Settings _settings;
        public Models.Settings Settings
        {
            get
            {
                if (_settings == null) _settings = new Models.Settings() { URL = "https://<org>.volunteerhub.com", User = "<user>", LastRunDate = DateTime.Now , EndRunDate = DateTime.Now };
                return _settings;
            }
            set { _settings = value; }
        }

        private VolunteerHubHelper _helper;
        private VolunteerHubHelper Helper
        {
            get
            {
                if (_helper == null) _helper = new VolunteerHubHelper(this.Settings);

                return _helper;
            }
        }

        private List<OmaticIO.Base.MetaField> _metaData;
        protected override IEnumerable<OmaticIO.Base.MetaField> InternalGetMetaData()
        {
            if (_metaData == null)
            {
                _metaData = this.Helper.MetaData;
            }

            return _metaData;
        }

        private RecordHelper _recordHelper;

        protected override OmaticIO.Base.Record InternalGetNextRecord()
        {

            return _recordHelper.GetNextRecord();
        }

        public override int RecordCount
        {
            get
            {
                return _recordHelper.Count;
            }
        }

        public override string Name
        {
            get { return "VolunteerHub"; }
        }

        public override Guid SourceID
        {
            get { return new Guid("B02B64B2-4F78-4D6E-A754-FA80220625B9"); }
        }

        public override Version Version
        {
            get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; }
        }

        public override bool SupportsRecordCount
        {
            get
            {
                return true;
            }
            protected set
            {
                //do nothing
            }
        }

        public override bool HasConfig
        {
            get
            {
                return true;
            }
            protected set
            {
                //do nothing
            }
        }

        public override System.Drawing.Image Icon
        {
            get
            {
                return Properties.Resources.VolunteerHub_icon;
            }
            protected set
            {
                base.Icon = value;
            }
        }
        
        public override OmaticIO.Base.SourceConfigResponse ShowConfig()
        {
            bool IsNewMapping = false;
            if (_settings == null)
            {
                IsNewMapping = true;
            }

            OmaticIO.Base.SourceConfigResponse response = new OmaticIO.Base.SourceConfigResponse();
            ViewModels.SettingsViewModel settingsVM = new ViewModels.SettingsViewModel(Settings);
            settingsVM.ShowDates = (IsNewMapping == false);

            Views.SettingsView settingsView = new Views.SettingsView();
            settingsVM.Adapter = this;
            settingsView.DataContext = settingsVM;
            settingsView.ShowDialog();
            if (settingsView.DialogResult == true)
            {
                _helper = null;  //reset it w/ new settings
                NotifyMetaDataChanged(new EventArgs());
                response.Valid = true;
            }
            return response;
        }

        public override bool ReadyToProcess
        {
            get
            {
                return Settings.Valid();
            }
            protected set
            {
                //nothing
            }
        }

        protected override void SetDefaultsBeforeImport()
        {
            this.ImportProfile.BeforeImport += BeforeImport;
            this.ImportProfile.AfterImport += AfterImport;
            this.ImportProfile.AfterConstituentSave += AfterConstitSave;
            this.Parameters="Start Date: " + Settings.LastRunDate.ToString() +  ", End Date: " + Settings.EndRunDate.ToString();
        }

        private void BeforeImport(OmaticIO.Base.Import.ImportEventArguments e)
        {
            _recordHelper = new RecordHelper(Helper, Settings);
        }

        private void AfterImport()
        {
            //Save the last run date
            //Settings.LastRunDate = DateTime.Now;
            this.Profile.Save();
            
            //_metaData = null;
        }

        private void AfterConstitSave(OmaticIO.Base.Import.ImportConstituentEventArguments e)
        {
            Settings.LastRunDate = _recordHelper.LastRunDate;
            this.Profile.Save();
        }

        public override void SetProfileMapping()
        {
            //provide default mapping suggestions for a newly created profile
            OmaticIO.Base.Import.ImportProfileBase profile = (OmaticIO.Base.Import.ImportProfileBase)this.Profile;
            FieldHelper.SetDefaultFieldMapping(profile);
        }

        public void Dispose()
        {
            this.ImportProfile.BeforeImport -= BeforeImport;
            this.ImportProfile.AfterImport -= AfterImport;
        }
    }
}
